/**
 * @author kzm
 * @version 1.6
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 
 */
package product{
	import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.utils.setTimeout;
    import flash.utils.clearTimeout;
	import flash.display.Stage;
    import flash.display.Sprite;
    import flash.events.ContextMenuEvent;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	
/** 
 * Machine generated class holding product meta info.
 */
	public final class Product {
		public static const NAME:String = "MediaCatalyst_ActionScript_Production_Template";
  		public static const RELEASE:String = "0";
  		public static const SPRINT:String = "0";
  		public static const TIMESTAMP:String = "2009.01.20-10:48";
  		public static const BUILD:Number = 96;
  		public static const COPYRIGHT:String = "Copyright(c) Sonyericsson(TM)";
		public static const OWNER:String = "http://www.mediacatalyst.com";
		public static const AUTHOR:String = "Niels";
		private static var KEY:uint = 192;
		private static var _USESEPERATOR:Boolean;
		
		/**
		 * To set the product info into the context menu (right mouse click) of the swf.
		 * @param useSeperator Decides if seperators are used between rows. 
		 */
		public static function createContextMenu(sprite:Sprite,useSeperator:Boolean=true):void{
			if(sprite.stage == null){
				_USESEPERATOR = useSeperator;
				sprite.addEventListener(Event.ADDED_TO_STAGE,_createContextMenuOnceAdded,false,0,true);
			} else
				_createContextMenu(sprite,useSeperator);
		}
				
		private static function _createContextMenuOnceAdded(e:Event):void{
			_createContextMenu(Sprite(e.target),_USESEPERATOR);
		}
				
		private static function _createContextMenu(sprite:Sprite,useSeperator:Boolean=true):void{		
			var menu:ContextMenu = new ContextMenu();
			menu.hideBuiltInItems();
			menu.builtInItems.print = true;
			var name:ContextMenuItem = new ContextMenuItem(NAME,useSeperator);
			menu.customItems.push(name);
			var version:ContextMenuItem = new ContextMenuItem("Ver."+RELEASE+"."+SPRINT);
	        menu.customItems.push(version);
	        version.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _showBuild);
			var copy:ContextMenuItem = new ContextMenuItem(COPYRIGHT);
			menu.customItems.push(copy);
			sprite.contextMenu = menu;
	        copy.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _goOwner);
		}
		/**
		 * Use every channel to display there is.
		 */
		public static function register(sprite:Sprite):void{
			createContextMenu(sprite);
			createTraceMessage();
			displayInfoOnKey(sprite);
		}
		/**
		 * Send product info to trace. 
		 */
		public static function createTraceMessage():void{
			trace(NAME);
			trace( "Product: "+NAME );
			trace( "Copyright: "+COPYRIGHT );
			trace( "Owner: "+OWNER );
			trace( "Release: "+RELEASE );
			trace( "Sprint: "+SPRINT );
			trace( "Author: "+AUTHOR );
			trace( "Build: "+BUILD );
			trace( "Timestamp: "+TIMESTAMP );
		}
		
		/**
		 * Show product info on key trigger. Note: any given key will be used in combination with shift and the alt key.
		 * @param sprite Needed to apply keys and draw to the stage.
		 * @param code charcode of the key. Default key is the tilde (~).
		 */
		public static function displayInfoOnKey(sprite:Sprite,code:uint=192):void{
			if(sprite.stage == null)
				sprite.addEventListener(Event.ADDED_TO_STAGE,_displayInfoOnKey,false,0,true);
			else
				sprite.stage.addEventListener(KeyboardEvent.KEY_UP,_react);
			KEY = code;
		}
		private static function _displayInfoOnKey(e:Event):void{
			Sprite(e.target).stage.addEventListener(KeyboardEvent.KEY_UP,_react);	
		}
		
		private static function _react(e:KeyboardEvent):void{
			if(e.altKey && e.shiftKey && e.keyCode == KEY)
				displayInfo(e.currentTarget as Stage);
		}
		/**
		 * Displays the product info in a sprite.
		 */
		public static function displayInfo(stage:Stage):void{
			if(stage == null) 
				throw new Error("Product::displayInfo: Given argument 'stage' can not be NULL.");
				
			stage.addEventListener(KeyboardEvent.KEY_UP,_react,false,0,true);
	
			if(stage.getChildByName(NAME) == null){
				var sprt:Sprite = new Sprite();
				var txt:TextField = new TextField();
				txt.wordWrap = false;
				txt.multiline = true;
				txt.autoSize = TextFieldAutoSize.LEFT;
				txt.appendText( "Product: "+NAME );
				txt.appendText( "\nCopyright: "+COPYRIGHT );
				txt.appendText( "\nOwner: "+OWNER );
				txt.appendText( "\nRelease: "+RELEASE );
				txt.appendText( "\nSprint: "+SPRINT );
				txt.appendText( "\nAuthor: "+AUTHOR );
				txt.appendText( "\nBuild: "+BUILD );
				txt.appendText( "\nTimestamp: "+TIMESTAMP );
				txt.setTextFormat(new TextFormat("_sans",10,0xffffff,true));
				txt.cacheAsBitmap = true;
				txt.x = 6;
				txt.y = 5;
				sprt.graphics.beginFill(0x666666,90);
				sprt.graphics.drawRect(0,0,txt.width + 12,txt.height + 10);
				sprt.addChild(txt);
				sprt.name = NAME;
				sprt.x = stage.stageWidth/2-sprt.width/2;					
				sprt.y = stage.stageHeight/2-sprt.height/2;
				stage.addChild(sprt);
				var o:TimeoutObject = new TimeoutObject();
				o.target = sprt;
				o.id = setTimeout(_closeDisplay,5000,o);
			}
		}

		private static function _closeDisplay(obj:TimeoutObject):void{
			clearTimeout(obj.id);
			obj.target.parent.removeChild(obj.target);
		}
		
		private static function _showBuild(e:ContextMenuEvent):void{
			navigateToURL(new URLRequest("javascript:alert('Build from "+AUTHOR+", No."+RELEASE+"."+SPRINT+"."+BUILD+", time:"+TIMESTAMP+"');void(0);"),"_self");
		}
		private static function _goOwner(e:ContextMenuEvent):void{
			navigateToURL(new URLRequest(OWNER),"owner");
		}
		/**
		 * The class can not be initialized.
		 * @throws StaticClassError
		 */	
		public function Product(){
			throw new Error("Product::Product: It is not permitted to initiate this abstract class.");
		}
	}
}

import flash.display.DisplayObject;

class TimeoutObject{
	public var id:uint;
	public var target:DisplayObject;
}